﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Intrastructure.Implement
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }
        public async Task<int> Add(T t)
        {
            //异常处理
            try
            {
                await db.Set<T>().AddAsync(t);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<int> Delete(T t)
        {
            //异常处理
            try
            {
                db.Set<T>().Remove(t);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IQueryable<T> GetAll()
        {
            //异常处理
            try
            {
                return db.Set<T>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<T> GetValue(int id)
        {
            //异常处理
            try
            {
                return await db.Set<T>().FindAsync(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<int> Update(T t)
        {
            //异常处理
            try
            {
                db.Set<T>().Update(t);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
